/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.net.greenet.dpiud_device.servlet;

import cn.net.greenet.common.db_service.BaseDao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * Ud 设备管理类
 *
 * @author ShiYun,2014/12/18
 */
public class DpiudManageService {

    public static final String QUERY = "SELECT * FROM `shj_db`.`dpiud_device` ";
    public static final String QUERYALL = "SELECT * FROM `shj_db`.`dpiud_device`";
    public static final String INSERT = "INSERT INTO `shj_db`.`dpiud_device`(device_name,ip,device_type) VALUES (?,?,?)";
    public static final String UPDATE = "UPDATE `shj_db`.`dpiud_device` SET`device_name` = ?, `ip` = ? , device_type =? WHERE `id` = ? ";
    public static final String DELETE = "DELETE FROM `shj_db`.`dpiud_device` WHERE `id` = ? ";
    private BaseDao dao = null;

    private void init() {
        dao = new BaseDao();
    }

    private void close() {
        if (dao != null) {
            dao.close();
        }
    }

    public boolean insert(String device_name, String ip, int type) {
        boolean flag = false;
        init();
        Object[] s = {device_name, ip, type};
        int num = dao.executeUpdate(INSERT, s);
        if (num != 0) {
            flag = true;
        }
        close();
        return flag;
    }

    public boolean delete(long id) {
        boolean flag = false;
        init();
        Object[] s = {id};
        int num = dao.executeUpdate(DELETE, s);
        if (num != 0) {
            flag = true;
        }
        close();
        return flag;
    }

    public boolean update(String device_name, String ip, int type, long id) {
        boolean flag = false;
        init();
        Object[] s = {device_name, ip, type, id};
        int num = dao.executeUpdate(UPDATE, s);
        if (num != 0) {
            flag = true;
        }
        close();
        return flag;
    }

    public JSONArray findAll() {
        init();
        JSONArray arr = new JSONArray();
        try {
            String[] s = {};
            ResultSet rs = dao.executeSQL(QUERYALL, s);
            while (rs.next()) {
                long id = rs.getLong(1);
                String device_name = rs.getString(2);
                String ip = rs.getString(3);
                int type = rs.getInt(4);
                JSONObject jsObj = new JSONObject();
                jsObj.put("id", id);
                jsObj.put("device_name", device_name);
                jsObj.put("ip", ip);
                jsObj.put("device_type", type);
                arr.add(jsObj);
            }
        } catch (SQLException ex) {
            Logger.getLogger(DpiudManageService.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            close();
        }
        return arr;
    }

    public static void main(String[] args) {
        DpiudManageService ud = new DpiudManageService();

        boolean f = ud.insert("test", "10.10.1.3", 1);
        System.out.println("Add:" + f);
        System.out.println("end:" + ud.findAll());
        boolean f2 = ud.insert("test2", "10.10.1.4", 1);
        boolean flag = ud.delete(1);
        System.out.println("Del:" + flag);
        boolean flag1 = ud.update("test2", "10.10.1.4", 1, 1);
        System.out.println("update:" + flag1);
        System.out.println("start:" + ud.findAll());

    }
}
